﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.BLL;
using WHC.Hotel.Entity;
using WHC.OrderWater.Commons;


namespace WHC.Hotel.UI
{
    public partial class FrmBookRoomNoFixNum :BaseForm
    {
        public FrmBookRoomNoFixNum()
        {
            InitializeComponent();
            InitDictItems();
            txtBookNo.Text = Public.GetPreOrderNumber();
            txtPreArrive.Value = DateTime.Now.Date;
            txtPreOff.Value = txtPreArrive.Value.AddDays(1);
        }

        private void InitDictItems()
        {
            this.txtRoomType.Items.Clear();
            List<RoomTypeInfo> typeList = BLLFactory<RoomType>.Instance.GetAll();
            foreach (RoomTypeInfo typeInfo in typeList)
            {
                this.txtRoomType.Items.Add(new CListItem(typeInfo.Name));
            }
            txtRoomType.SelectedIndex = 0;
            lbRoomType.Text = txtRoomType.Text;

            this.txtSource.Items.Clear();
            this.txtSource.Items.AddRange(DictItemUtil.GetCustomerSource());
            if (this.txtSource.Items.Count > 0)
            {
                this.txtSource.SelectedIndex = 0;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {


            if (this.txtBookPeople.Text.Length == 0)
            {
                MessageUtil.ShowTips("预定人不能为空");
                this.txtBookPeople.Focus();
                return;
            }
            if (this.txtName.Text.Length == 0)
            {
                MessageUtil.ShowTips("宾客名称不能为空");
                this.txtName.Focus();
                return;
            }
            if (this.txtTelephone.Text.Length == 0)
            {
                MessageUtil.ShowTips("联系电话不能为空");
                this.txtTelephone.Focus();
                return;
            }
            if (txtPreArrive.Value >= txtPreOff.Value)
            {


                MessageUtil.ShowTips("预离时间必须大于预抵达时间");
                this.txtPreOff.Focus();
                return;
            
            
            }

            try
            {

                
                    BookInfo mbookInfo = new BookInfo();
                    mbookInfo.RoomNo = "未指定";
                    mbookInfo.BookNo = txtBookNo.Text;
                    mbookInfo.RoomType = txtRoomType.Text;
                    mbookInfo.CustomerCompany = this.txtCompany.Text;
                    mbookInfo.CustomerName = this.txtName.Text;
                    mbookInfo.CustomerTel = this.txtTelephone.Text;
                    mbookInfo.Note = this.txtNote.Text;
                    mbookInfo.PreArrivalTime = this.txtPreArrive.Value;
                    mbookInfo.PreOffTime = this.txtPreOff.Value;
                    mbookInfo.Source = this.txtSource.Text;
                    mbookInfo.BookPeople = txtBookPeople.Text;
                    mbookInfo.BookPeoplePhone = txtBookPeoplePhone.Text;
                    mbookInfo.IsFixRoomNo = false;
                    mbookInfo.RoomCount = Convert.ToInt32(txtRoomCount.Text);
                    mbookInfo.Note = txtRemark.Text;
                    mbookInfo.IsTeam = 0;

                    BLLFactory<Book>.Instance.Insert(mbookInfo);

              
                MessageUtil.ShowTips("预定客房成功");
                this.DialogResult = DialogResult.OK;

            }
            catch (Exception ex)
            {

                LogHelper.Error(ex);
                MessageUtil.ShowError(ex.Message);
            
            
            }

        }

        /// <summary>
        /// 显示可用房源列表
        /// </summary>
        private void ShowRoomCanUseInfo()
        {
            DataGridViewTextBoxColumn column=null;
            dgView.Columns.Clear();
            DataTable roomdt = new DataTable();
            DateTime arriveDate = txtPreArrive.Value;
            DateTime OffDate = txtPreOff.Value;
            int dayCount = (OffDate - arriveDate).Days + 1;
            for (int i = 0; i < dayCount; i++)
            {
                column = new DataGridViewTextBoxColumn();
                column.HeaderText = arriveDate.AddDays(i).ToString("yyyy.MM.dd");
                column.DisplayIndex = i;
                column.Width = 80;
                column.DataPropertyName = arriveDate.AddDays(i).ToString("yyyy.MM.dd");
                column.Name = arriveDate.AddDays(i).ToString("yyyy.MM.dd");
                column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                dgView.Columns.Add(column);
                roomdt.Columns.Add(arriveDate.AddDays(i).ToString("yyyy.MM.dd"), typeof(int));
            
            
            }

            //生成数据
            List<RoomInfo> roomlist = BLLFactory<WHC.Hotel.BLL.Room>.Instance.FindAllRooms("  RoomType='"+txtRoomType.Text+"' ");
            List<BookInfo> bookList = BLLFactory<WHC.Hotel.BLL.Book>.Instance.FindBookByCondition("  RoomType='" + txtRoomType.Text + "' ");
            DataRow dr = roomdt.NewRow();
            //初始化
            for (int j = 0; j < dayCount; j++)
            {

                dr[arriveDate.AddDays(j).ToString("yyyy.MM.dd")] = 0;

            }
            int sum = roomlist.Count;//总房数;
            int canUse = 0;
            int bookNum = 0;
            for (int k = 0; k < dayCount; k++)
            {
                
                //1:判断当天可能的房间数
                canUse = 0;
                foreach (RoomInfo entity in roomlist)
                {



                    switch (entity.Status)
                    {
                        case Entity.RoomStatus.空净房:
                        case Entity.RoomStatus.空脏房:
                            canUse++;
                            break;
                        case Entity.RoomStatus.住净房:
                        case Entity.RoomStatus.住脏房:
                            if (!(arriveDate.AddDays(k) >= entity.OrderInfo.CreateDate && arriveDate.AddDays(k) < entity.OrderInfo.PreOffDate))
                            {

                                canUse++;

                            }
                            break;
                        case Entity.RoomStatus.预定房:
                             canUse++;
                             break;


                    }



                }//判断当天可能的房间数end


                //2:扣除当天预订数
                if (bookList.Count > 0)
                {
                    foreach (BookInfo item in bookList)
                    {

                        if (arriveDate.AddDays(k) >= item.PreArrivalTime && arriveDate.AddDays(k) < item.PreOffTime)
                        {


                            bookNum = bookNum + item.RoomCount;//当天需要天数


                        }

                    }

                }
               
                //3.统计
                dr[arriveDate.AddDays(k).ToString("yyyy.MM.dd")] = canUse - bookNum;


            }


      

            roomdt.Rows.Add(dr);
            this.dgView.DataSource = roomdt;


         
        
        }

        private void txtRoomType_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShowRoomCanUseInfo();
            lbRoomType.Text = txtRoomType.Text;

        }

        private void txtPreArrive_ValueChanged(object sender, EventArgs e)
        {
            ShowRoomCanUseInfo();
        }

        private void txtPreOff_ValueChanged(object sender, EventArgs e)
        {
            ShowRoomCanUseInfo();
        }

        private void FrmBookRoomNoFixNum_Load(object sender, EventArgs e)
        {
            ShowRoomCanUseInfo();
        }
    }
}
